<#-- 
	Common macro libraries for MySQL DAO
	revision: 2006/08/26
-->
<#-- 
	MACRO:  print_pkey
	Prints a comma separated list of primary keys (column names)
-->
<#macro  print_pkey xmlClassElement prefix delim=", ">
<#assign ctr=0>
<#list xmlClassElement.column as column>
<#if ctr==0><#assign comma=""><#else><#assign comma = delim></#if>
<#if column.@primaryKey[0] == "true" >
<#assign ctr=ctr+1>${comma}${prefix}${column.@name}</#if></#list></#macro>

<#-- 
	MACRO:  print_prop
	Prints a column/property name 
	@prefix = prefix for each column/property name
	@withEscape = wheather to apply the sql escaping funcion or not
-->
<#macro print_prop columnElement prefix suffix="" withEscape=true>
<#if withEscape><@escape_string varname=columnElement.@name type=columnElement.@type prefix=prefix suffix=suffix /><#else>${prefix}${columnElement.@name}</#if></#macro>

<#-- 
	MACRO:  print_props
	Prints a list of column/property name separated by commas
	Used for SELECT and INSERT.
	@prefix = prefix for each column/property name
	@withEscape = wheather to apply the sql escaping funcion or not
-->
<#macro print_props xmlClassElement prefix mode withEscape=false delim=", ">
<#assign ctr=0 >
<#list xmlClassElement.column as column>
<#if ctr==0><#assign comma=""><#else><#assign comma = delim></#if>
<#if mode?contains("p") && column.@primaryKey == "true"><#assign ctr=ctr+1>${comma}<@print_prop columnElement=column prefix=prefix withEscape=withEscape /></#if><#if mode?contains("n") && column.@primaryKey != "true"><#assign ctr=ctr+1>${comma}<@print_prop columnElement=column prefix=prefix withEscape=withEscape /></#if></#list></#macro>

<#-- 
	MACRO:  print_repeatstring
	Prints a repetition of the given string
	@count = number of times to repeat
	@repeatstr = the string to repeat
-->
<#macro print_repeatstring count repeatstr>
<#list 1..count as i>${repeatstr} </#list></#macro>

<#-- 
	MACRO:  print_typechar
	Prints the type format (specific to MySQLi library: 1, d or s)
	Used when binding host variables.
	@type = SQL type 
-->
<#macro print_typechar type>
<#if type?upper_case=="LONG" || type?upper_case=="INT" || type?upper_case=="INTEGER" || type?upper_case=="SHORT">i<#elseif type?upper_case=="FLOAT" || type?upper_case=="DOUBLE">d<#elseif type?upper_case=="CHAR" || type?upper_case=="VARCHAR" || type?upper_case=="STRING" || type?upper_case=="TEXT" || type?upper_case=="DATE" || type?upper_case=="TIMESTAMP">s<#elseif type?upper_case=="BINARY" || type?upper_case=="BIN">b</#if></#macro>

<#-- 
	MACRO:  print_bparamtchars
	Prints the bind parameter characters in sequence for all the columns
	@mode = wheather to include the pk column or not (PENDING)
-->
<#macro print_bparamtchars xmlClassElement mode>
<#list xmlClassElement.column as column>
<@print_typechar type=column.@type />
</#list>
</#macro>

<#-- 
	MACRO:  escape_string
	Prints the variable wrapped with escape function if it is a string type,
	the same variable name otherwise.
	@varname = the name of the variable with out the '$'
	@prefix = the prefix for the variable i.e. '$'
-->
<#macro escape_string varname type prefix="$" suffix="" apply=true>
<#if apply && (type?upper_case=="CHAR" || type?upper_case=="VARCHAR" || type?upper_case=="STRING" || type?upper_case=="TEXT" || type?upper_case=="DATE" || type?upper_case=="TIMESTAMP") >'". ALT_MySQLDAOBase::sql_quote(${prefix}${varname}${suffix})."'<#else>${prefix}${varname}${suffix}</#if></#macro>

<#-- 
	MACRO:  print_assigns
	Prints the list of column - property assignment (i.e.: myCol = $myCol)
	Used in the UPDATE statement.
	@xmlClassElement = the name of the variable with out the '$'
	@prefix = the prefix for the variable i.e. '$'
	@mode = 'p' includes primary key columns, 'n' includes non-primary key columns
	@isBinding if true prints a question mark '?' (for mysqli)
-->
<#macro print_assigns xmlClassElement mode prefix suffix="" isBinding=false applyEscape=false >
<#assign ctr=0 >
<#list xmlClassElement.column as column >
<#if ctr==0><#assign comma=""><#else><#assign comma = ", "></#if>
<#if mode?contains("p") && column.@primaryKey == "true"><#assign ctr=ctr+1>${comma} ${column.@name} = <#if isBinding>?<#else><@escape_string varname=column.@name type=column.@type prefix=prefix suffix=suffix apply=applyEscape /></#if></#if><#if mode?contains("n") && column.@primaryKey != "true"><#assign ctr=ctr+1>${comma} ${column.@name} = <#if isBinding>?<#else><@escape_string varname=column.@name type=column.@type prefix=prefix suffix=suffix apply=applyEscape /></#if></#if></#list></#macro>

<#-- 
	MACRO:  print_colnames_csv
	Prints the list of column - property assignment (i.e.: myCol = $myCol)
	Used in the UPDATE statement.
	@xmlClassElement = the name of the variable with out the '$'
	@prefix = the prefix for the variable i.e. '$'
	@isBinding if true prints a question mark '?' (for mysqli)
-->
<#macro print_colnames_csv xmlClassElement >
<#assign ctr=0 ><#list xmlClassElement.property as column ><#if ctr==0><#assign comma=""><#else><#assign comma = ", "></#if><#assign ctr=ctr+1>${comma} "${column.@name}"</#list><#list xmlClassElement["many-to-one"] as column ><#if ctr==0><#assign comma=""><#else><#assign comma = ", "></#if><#assign ctr=ctr+1>${comma} "${column.@column}"</#list></#macro>